package LeetCode.day1;

/**
 * @description:
 * @author: Cter
 * @date: 2020/6/19
 */
public class Solution2 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode root = new ListNode(0);
        ListNode c = root;
        int carry= 0;
        while (l1!=null || l2!=null || carry!=0){
            int l1Val = l1!=null?l1.val:0;
            int l2Val = l2!= null?l2.val :0;
            int sumVal = l1Val + l2Val + carry;
            carry = sumVal/10;

            ListNode sumNode = new ListNode(sumVal%10);
            c.next = sumNode;
            c = sumNode;

            if(l1 != null) l1 = l1.next;
            if(l2 != null) l2 = l2.next;
        }
        return root.next;
    }
}
